package myJs.components

import japgolly.scalajs.react.{Reusability, ScalaComponent}
import japgolly.scalajs.react.component.Scala.BackendScope
import japgolly.scalajs.react.extra.StateSnapshot
import japgolly.scalajs.react.vdom.all._
import monocle.Lens
import myJs.components.inline.SelectInlineComponent
import myJs.tool.Tool._
import japgolly.scalajs.react.ReactMonocle._
import monocle.macros.Lenses

import scala.collection.immutable.SeqMap
import myJs.Implicits._
import myJs.tool.Tool

/**
 * Created by yz on 3/11/2020
 */
object GroupSelectComponent {

  @Lenses
  case class State(v: String, show: Boolean, groups: List[String], labelWidth: Int, inputWidth: Int)

  object State {

    def init = {

      State(v = "", show = false, groups = List[String](), labelWidth = 3, inputWidth = 6)
    }

  }

  class Backend(bs: BackendScope[Unit, State]) {

    def render(s: State) = {

      implicit class MyLens[T](lens: Lens[State, T]) {

        def zoomL(implicit r: Reusability[T]) = {
          StateSnapshot.withReuse.zoomL(lens).prepareVia(bs).apply(s)
        }

      }
      val groupVsMap =Tool.getGroupVs(s.groups).optionMap

      div(
        SelectInlineComponent.Props(State.v.zoomL, label = messages("group"), name = "group", optionMap = groupVsMap,
          show = s.show, labelWidth = s.labelWidth, inputWidth = s.inputWidth).render
      )

    }

  }

  val Component = ScalaComponent.builder[Unit].initialState(State.init).renderBackend[Backend].build


}
